package com.mfsoft.net;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Paint.Join;

import com.mfsoft.model.*;
public class MyFunctions_THI {

	JSONParser_THI jsonparser_thi;
	String loginurl = "http://ungdungviet.zz.mu/camera/note.php";
	public static final String registerurl = "http://ungdungviet.zz.mu/camera/note.php";
	String login_tag = "login";
	String register = "register";
	String prefname = "my_data";
	String updateinfor = "updateinfor";
	Context context;
	// ham khoi tao doi tuong jsonparser
	public MyFunctions_THI(Context context)
	{
		jsonparser_thi = new JSONParser_THI();
		this.context = context;
	}
	
	// doc tu shared neu da log neu chua log tra ve false, log roi tra ve true
	public boolean checkLogin()
	{
		SharedPreferences lay = context.getSharedPreferences(prefname, context.MODE_WORLD_READABLE);
		String usernamelogined = lay.getString("username", "chua login");
		if(usernamelogined.equals("chua login"))
			return false;
		else
			return true;
	}
	/*public void removeSharedPreferences() {

		
		SharedPreferences pre=context.getSharedPreferences(prefname, context.MODE_PRIVATE);
				 //tạo đối tượng Editor để lưu thay đổi
		SharedPreferences.Editor edit=pre.edit();
		//SharedPreferences preferences = null;
		edit = pre.edit();
		edit.remove("username");
		edit.commit();
	}*/
	// lay email da login
	public String getUsername()
	{
		SharedPreferences lay = context.getSharedPreferences(prefname, context.MODE_WORLD_READABLE);
		String usernamelogined = lay.getString("username", "");
		return usernamelogined;
	}
	// ghi du lieu lai cho emaillogined thanh "chua login"
	public boolean LogOut()
	{
		SharedPreferences ghi = context.getSharedPreferences(prefname, context.MODE_WORLD_WRITEABLE);
		SharedPreferences.Editor editor = ghi.edit();
		editor.clear();
		editor.commit();
		return true;
	}
	
	//khi da login thi luu uername
	public boolean setUsernameLogin(String username)
	{
		SharedPreferences ghi = context.getSharedPreferences(prefname, context.MODE_WORLD_WRITEABLE);
		SharedPreferences.Editor editor = ghi.edit();
		editor.putString("username", username);
		
		editor.commit();
		return true;
	}
	/*nháº­n dá»¯ liá»‡u gá»�i hÃ m vÃ  tráº£ vá»� json
	nÃªn chÃº Ã½ ArrayList vÃ  BasicNamValuePair*/
	public JSONObject loginUser(String username , String password)
	{
		List<NameValuePair> cacdoiso = new ArrayList<NameValuePair>();
		cacdoiso.add(new BasicNameValuePair("command", login_tag));//la command khong phai tag.
		cacdoiso.add(new BasicNameValuePair("username", username));
		cacdoiso.add(new BasicNameValuePair("password", password));
		JSONObject jobj = jsonparser_thi.getJSONFromURL(loginurl, cacdoiso);
		setUsernameLogin(username); // cÃ¡i nÃ y gáº¯n lÃªn share Ä‘á»ƒ nhá»› Ä‘Ã£ login rá»“i 
		return jobj;
		
	}
	
	public JSONObject UpdateMyProfile(
			String tendangnhap,
			String diachiemail,
			String newPassWord,
			String gender,
			String ngaysinh)
	{
		List<NameValuePair>cacdoiso = new ArrayList<NameValuePair>();
		cacdoiso.add(new BasicNameValuePair("command", updateinfor));
		cacdoiso.add(new BasicNameValuePair("username", tendangnhap));
		cacdoiso.add(new BasicNameValuePair("gmail", diachiemail));
		cacdoiso.add(new BasicNameValuePair("password", newPassWord));
		cacdoiso.add(new BasicNameValuePair("gender", gender));
		cacdoiso.add(new BasicNameValuePair("birthday", ngaysinh));
		
		JSONObject jobj = jsonparser_thi.getJSONFromURL(registerurl, cacdoiso);
		return jobj;
	}
	
	public JSONObject NoUpdateMyProfile(
			String tendangnhap,
			String diachiemail,
			String Oldpasword,
			String gender,
			String ngaysinh)
	{
		List<NameValuePair>cacdoiso = new ArrayList<NameValuePair>();
		cacdoiso.add(new BasicNameValuePair("command", updateinfor));
		cacdoiso.add(new BasicNameValuePair("username", tendangnhap));
		cacdoiso.add(new BasicNameValuePair("gmail", diachiemail));
		cacdoiso.add(new BasicNameValuePair("password", Oldpasword));
		cacdoiso.add(new BasicNameValuePair("gender", gender));
		cacdoiso.add(new BasicNameValuePair("birthday", ngaysinh));
		
		JSONObject jobj = jsonparser_thi.getJSONFromURL(registerurl, cacdoiso);
		return jobj;
	}
	//Nháº­n dá»¯ liá»‡u Ä‘Äƒng kÃ½
	public JSONObject registerUser(String hoten,
			String tendangnhap,
			String diachiemail,
			String pasword,
			String gender,
			String ngaysinh)
	{
		List<NameValuePair>cacdoiso = new ArrayList<NameValuePair>();
		cacdoiso.add(new BasicNameValuePair("command", register));
		cacdoiso.add(new BasicNameValuePair("nickname", hoten));
		cacdoiso.add(new BasicNameValuePair("username", tendangnhap));
		cacdoiso.add(new BasicNameValuePair("gmail", diachiemail));
		cacdoiso.add(new BasicNameValuePair("password", pasword));
		cacdoiso.add(new BasicNameValuePair("gender", gender));
		cacdoiso.add(new BasicNameValuePair("birthday", ngaysinh));
		
		JSONObject jobj = jsonparser_thi.getJSONFromURL(registerurl, cacdoiso);
		return jobj;
	}
	
}
